<template>
  <div class="app-container">
    <div class="content-container">
      <el-card shadow="never" class="table-card">
        <div class="flex-x-between mb-10px">
          <div>
            <el-button type="success" icon="plus" @click="handleOpenDialog(null, 'add')">
              新增
            </el-button>
          </div>
        </div>
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :pageSize="queryParams.pageSize"
            :total="total"
            :loading="loading"
            @handle-query="handleChangePage"
          >
            <template #index="scope">
              {{ scope.index + 1 }}
            </template>
            <template #operation="{ row, name }">
              <el-button link type="primary" icon="Edit" @click="handleOpenDialog(row, 'edit')">
                编辑
              </el-button>
              <el-button link type="primary" icon="View" @click="handleOpenDialog(row, 'look')">
                查看
              </el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(row.id)">
                删除
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="1000px"
      @close="handleCloseDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="computedRules"
        label-width="100px"
        :disabled="isDisabled"
      >
        <el-form-item label="KEY" prop="agreementKey">
          <el-input v-model="formData.agreementKey" placeholder="请输入KEY" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <div style="height: calc(100vh - 680px); overflow-y: scroll" class="editor-box">
            <WangEditor v-model="formData.content" :disabled="isDisabled" />
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmitClick" :disabled="isDisabled">
            确 定
          </el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import agreementApi from "@/api/system/agreement";
const tableData = ref([]);
const tableColumnsData = ref([
  {
    prop: "index",
    label: "排序",
    showOverflowTooltip: true,
    width: 150,
    customSlot: "index",
  },
  {
    prop: "agreementKey",
    label: "KEY",
    showOverflowTooltip: true,
  },
  {
    prop: "title",
    label: "标题",
    showOverflowTooltip: true,
  },
  {
    prop: "operation",
    label: "操作",
    showOverflowTooltip: true,
    width: 300,
    customSlot: "operation",
  },
]);
const total = ref(0);
const loading = ref(false);
const dialog = ref({
  visible: false,
  title: "新增",
});
const formData = ref({
  agreementKey: "",
  title: "",
  content: "",
  id: "",
});
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
});
const computedRules = ref({
  agreementKey: [{ required: true, message: "请输入key值", trigger: "blur" }],
  title: [{ required: true, message: "请输入标题", trigger: "blur" }],
  content: [{ required: true, message: "请输入内容", trigger: "change" }],
});
const isDisabled = ref(false);
const dataFormRef = ref();

async function getList() {
  try {
    loading.value = true;
    const data = await agreementApi.getList(queryParams.value);
    tableData.value = data.data;
    total.value = data.total;
  } catch (err) {
  } finally {
    loading.value = false;
  }
}

function handleOpenDialog(val, type) {
  if (val) {
    formData.value = { ...val };
    dialog.value.title = "编辑";
  } else {
    formData.value.id = undefined;
    dialog.value.title = "新增";
  }
  if (type === "look") {
    dialog.value.title = "查看";
    isDisabled.value = true;
  } else {
    isDisabled.value = false;
  }
  dialog.value.visible = true;
}

async function handleDelete(id) {
  const result = await ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });
  if (!result) return;
  try {
    loading.value = true;
    await agreementApi.delData(id);
    ElMessage.success("删除成功");
    getList();
  } catch (e) {
    console.log(e);
  } finally {
    loading.value = false;
  }
}
function handleChangePage({ pageNum, pageSize }) {
  queryParams.value.pageNum = pageNum;
  queryParams.value.pageSize = pageSize;
  getList();
}
// 关闭
function handleCloseDialog() {
  dataFormRef.value.resetFields();
  dialog.value.visible = false;
  // dataFormRef.value.clearValidate();
}
// 提交
async function handleSubmitClick() {
  dataFormRef.value.validate(async (isValid) => {
    if (isValid) {
      if (formData.value.content == "<p><br></p>" || formData.value.content == "<p></p>") {
        ElMessage.error("请输入内容");
        return;
      }
      try {
        loading.value = true;
        await agreementApi
          .addForm(formData.value)
          .then((res) => {
            ElMessage.success(dialog.value.title + "成功");
            handleCloseDialog();
            getList();
          })
          .finally(() => {
            loading.value = false;
          });
      } catch (err) {}
    }
  });
}

onMounted(() => {
  getList();
});
</script>
